package net.cctms.zhishu.webAppFramework.utils;

import android.text.TextUtils;


import net.cctms.zhishu.MyApplication;


/**
 * 用户信息操作类
 *
 *
 * 该类删除了大部分代码
 * <p/>
 * Created by yf on 2017/8/8.
 */
public class UserInfoUtil {

    private static final String ENCRYPT_KEY = "kanjian_key"; //本地保存身份证，电话号码等信息的加密key

    /**
     * 获取用户的所有信息
     *
     * @return
     */
    public static String getUserInfo() {
        return null; //被删了部分代码
    }

    public synchronized static void saveBgImgUrl(String url){
        DefaultShared.putString(Constants.KEY_BGURL, url);
    }

    /**
     * 保存用户的accessToken
     *
     * @param accessToken
     */
    public synchronized static void saveUserAccessToken(String accessToken) {
        DefaultShared.putString(Constants.KEY_ACCESSTOKEN, accessToken);
    }

    /**
     * 获取用户的accessToken
     *
     * @return
     */
    public static String getUserAccessToken() {
        return DefaultShared.getString(Constants.KEY_ACCESSTOKEN, null);
    }

    /**
     * 保存用户的ExpiresIn
     *
     * @param expiresIn
     */
    public synchronized static void saveUserExpiresIn(long expiresIn) {
        DefaultShared.putLong(Constants.KEY_EXPIRESIN, expiresIn);
    }

    /**
     * 获取用户的ExpiresIn
     *
     * @return
     */
    public static long getUserExpiresIn() {
        return DefaultShared.getLong(Constants.KEY_EXPIRESIN, 0L);
    }


    /**
     * 校验用户是否登录
     *
     * @return
     */
    public static boolean isTianYiLogon() {
        boolean result = false;
        String token = getUserAccessToken();
        if (!TextUtils.isEmpty(token)) {
            long expiresIn = getUserExpiresIn();
            if (System.currentTimeMillis() < expiresIn) { //没有过期
                result = true;
            }
        }
        return result;
    }

    public static boolean isExpires() {
        long expiresIn = getUserExpiresIn();
        return System.currentTimeMillis() >= expiresIn;
    }






    /**
     * 保存用户的签名
     *
     * @param userSign
     */
    public synchronized static void saveUserSign(String userSign) {
        try{
            String key = Constants.KEY_SIGN;
            DefaultShared.putString(key, userSign);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取用户的签名
     *
     * @return
     */
    public static String getUserSign() {
        String key = Constants.KEY_SIGN;
        return DefaultShared.getString(key, "");
    }

    /**
     * 保存用户的IconUrl
     *
     * @param iconUrl
     */
    public synchronized static void saveUserIconUrl(String iconUrl) {
        String key = Constants.KEY_USER_ICON_URL;
        DefaultShared.putString(key, iconUrl);
    }

    /**
     * 获取用户的IconUrl
     *
     * @return
     */
    public static String getUserIconUrl() {
        String key = Constants.KEY_USER_ICON_URL;
        return DefaultShared.getString(key, null);
    }



    /**
     * 保存用户ID
     *
     * @return
     */
    public static void saveUserId(int userId) {
        DefaultShared.putInt(Constants.KEY_USER_ID, userId);
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public static int getUserId() {
        return DefaultShared.getInt(Constants.KEY_USER_ID, 0);
    }

    /**
     * 保存用户OpenID
     *
     * @return
     */
    public static void saveOpenId(String openId) {
        DefaultShared.putString(Constants.OPEN_ID, openId);
    }

    /**
     * 获取OpenID
     *
     * @return
     */
    public static String getOpenId() {
        String openid = DefaultShared.getString(Constants.OPEN_ID, "");
        Log.i("UserInfoUtil", "openId: " + openid);
        return openid;
    }



    /**
     * 获取启动图数据
     *
     * @return
     */
    public static String getStartImageData() {
        String startImageData = DefaultShared.getString(Constants.KEY_START_IMAGE, "");
        Log.i("UserInfoUtil", "startImageData: " + startImageData);
        return startImageData;
    }

    /**
     * 设置启动图数据
     *
     * @return
     */
    public static void setStartImageData(String startImageData) {
        DefaultShared.putString(Constants.KEY_START_IMAGE, startImageData);
    }

    /**
     * 保存游客OpenID
     *
     * @return
     */
    public static void saveGuestOpenId(String openId) {
        DefaultShared.putString(Constants.GUEST_OPEN_ID, openId);
    }

    /**
     * 获取游客OpenID
     *
     * @return
     */
    public static String getGuestOpenId() {
        String openid = DefaultShared.getString(Constants.GUEST_OPEN_ID, "");
        return openid;
    }




}
